package main

import (
	"fmt"
	"github.com/gogf/gf/os/glog"
	"github.com/samber/do"
	"gitlab.local/DO-module/new-filemanage-module/pkg"
	"io"
	"os"
	"time"
)

func isBeingDebugged() bool {
	file, err := os.Open("/proc/self/status")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	for {
		var tpid int
		num, err := fmt.Fscanf(file, "TracerPid: %d\n", &tpid)
		if err == io.EOF {
			break
		}
		if num != 0 {
			return true
		}
	}
	return false
}

func waitForDebugger() {
	glog.Info("waiting for debugger")
	for {
		if isBeingDebugged() {
			// wait for one more second just in case our breakpoint won't hit
			time.Sleep(time.Second * 5)
			break
		}
		time.Sleep(time.Second)
	}
	glog.Info("debugger is ready")
}

// InitPlugin : init package
func InitPlugin(injector *do.Injector) {
	waitForDebugger()
	pkg.InitModule(injector)
}

// main : a plugin must have a main function
func main() {}
